<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="iOS 中的系统常用宏定义NSObjCRuntime.h"><meta name="keywords" content="iOS"><meta name="author" content="kyren"><meta name="copyright" content="kyren"><title>iOS 中的系统常用宏定义NSObjCRuntime.h | Kyren's Blog</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cf87fcdbd9ab7cf93d894f7c219ae78f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script src="https://v1.hitokoto.cn/?encode=js&amp;charset=utf-8&amp;select=.footer_custom_text" defer></script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '6.2.0'
} </script><meta name="generator" content="Hexo 6.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://avatars.githubusercontent.com/u/5827823?v=4"></div><div class="author-info__name text-center">kyren</div><div class="author-info__description text-center">huluo的技术博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://github.com/huluo666">Follow Me</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">239</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">41</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">29</span></a></div><hr><div class="author-info-links"><div class="author-info-links__title text-center">Links</div><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:9090/">Jira</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:8090/">Confluence</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://bloc.leniugame.com/">Leniu</a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">Kyren's Blog</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">主页</a><a class="site-page" href="/essay">随笔</a><a class="site-page" href="/archives">归档</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a><a class="site-page" href="/about">关于</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">iOS 中的系统常用宏定义NSObjCRuntime.h</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2016-04-15</time></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>下列宏定义查看API：NSObjCRuntime.h,CGBase.h</p>
<p>当我们在 objectivec 中引入了 <code>Foundation Framework</code> 之后，最好是使用系统宏定义，比如用<code>FOUNDATION_EXPORT</code> 来代替 <code>extern</code>, 因为在 <code>NSObjCRuntime.h</code> 中它包括了一些 C 和 C++ 的库，为了能更好的和这些 C 和 C++ 库兼容，所以建议用 <code>FOUNDATION_EXPORT</code>。</p>
<p><code>内联函数</code> （<code>NS_INLINE</code>）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NS_INLINE</span><br><span class="line">//UIKit框架 UIKIT_STATIC_INLINE</span><br><span class="line">static inline CGRect ScaleRect(CGRect rect, float n)</span><br></pre></td></tr></table></figure>



<p><code>定义常量</code> （<code>FOUNDATION_EXPORT &amp; #define</code>）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);</span><br><span class="line">//UIKit框架 `UIKIT_EXTERN</span><br><span class="line">.h  FOUNDATION_EXPORT NSString * const kMyConstantString;</span><br><span class="line">.m  NSString * const kConstantString = @&quot;Hello&quot;;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#define kConstantString @&quot;Hello&quot;</span><br></pre></td></tr></table></figure>

<p><code>FOUNDATION_EXPORT</code>可以直接比较字符串指针地址，效率<code>#define</code>比高</p>
<p>UIKit框架 <code>UIKIT_EXTERN</code></p>
<p>取最大值、最小值、绝对值</p>
<pre><code>#define MIN(A,B)	((A) &lt; (B) ? (A) : (B))
#define MAX(A,B)	((A) &gt; (B) ? (A) : (B))
#define ABS(A)		((A) &lt; 0 ? (-(A)) : (A))
</code></pre>
<p>iOS常用示例</p>
<p>计算最大值、最小值、绝对值</p>
<p>define MIN(A,B) ((A) &lt; (B) ? (A) : (B))</p>
<p>define MAX(A,B) ((A) &gt; (B) ? (A) : (B))</p>
<p>define ABS(A)  ((A) &lt; 0 ? (-(A)) : (A))</p>
<p>&#x2F;&#x2F;计算scrollView索引</p>
<p>int index &#x3D; ABS(scrollView.contentOffset.x) &#x2F; scrollView.frame.size.width;</p>
<p> </p>
<p> <code>ABS</code></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">int <span class="built_in">abs</span>(int i);                  <span class="comment">// 处理int类型的取绝对值</span></span><br><span class="line">double fabs(double i); 			 <span class="comment">//处理double类型的取绝对值</span></span><br><span class="line">float fabsf(float i);            <span class="comment">//处理float类型的取绝对值</span></span><br><span class="line"></span><br><span class="line">double a <span class="operator">=</span> <span class="operator">-</span><span class="number">2.6</span>;</span><br><span class="line"><span class="type">NSLog</span>(@<span class="string">&quot;%.2f&quot;</span>,<span class="type">ABS</span>(a));<span class="comment">//2.60</span></span><br><span class="line">int b <span class="operator">=</span> <span class="operator">-</span><span class="number">4.0</span>;</span><br><span class="line"><span class="type">NSLog</span>(@<span class="string">&quot;%d&quot;</span>,<span class="type">ABS</span>(b));<span class="comment">//4</span></span><br></pre></td></tr></table></figure>



<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">  CGFloat maxSide = MAX(fabsf(point1.x - point2.x ), fabsf(point2.y - point2.y));</span><br></pre></td></tr></table></figure>

<p>四舍五入</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">roundf(<span class="operator">&lt;</span>#float#<span class="operator">&gt;</span>)</span><br><span class="line">指定精度取整函数: round</span><br><span class="line">语法: round(double a, int d)</span><br><span class="line">返回值: <span class="type">DOUBLE</span></span><br><span class="line">说明: 返回指定精度d的double类型</span><br><span class="line"></span><br><span class="line">举例：</span><br><span class="line">round(<span class="number">3.1415926</span>,<span class="number">4</span>)   <span class="comment">//3.1416</span></span><br></pre></td></tr></table></figure>

<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//    取整</span></span><br><span class="line">    double f <span class="operator">=</span><span class="number">2.5</span>;</span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">&quot;%.0f&quot;</span>,round(f));<span class="comment">//3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//    向下取整</span></span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">&quot;%f&quot;</span>,floor(f));<span class="comment">//2.000000</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//  向上取整</span></span><br><span class="line">    <span class="type">NSLog</span>(@<span class="string">&quot;%f&quot;</span>,ceil(f));</span><br></pre></td></tr></table></figure>



<p>rand() —-随机数</p>
<p>abs() &#x2F; labs() —-整数绝对值</p>
<p>fabs() &#x2F; fabsf() &#x2F; fabsl() —-浮点数绝对值</p>
<p>floor() &#x2F; floorf() &#x2F; floorl() —-向下取整</p>
<p>ceil() &#x2F; ceilf() &#x2F; ceill() —-向上取整</p>
<p>round() &#x2F; roundf() &#x2F; roundl() —-四舍五入</p>
<p>sqrt() &#x2F; sqrtf() &#x2F; sqrtl() —-求平方根</p>
<p>fmax() &#x2F; fmaxf() &#x2F; fmaxl() —-求最大值</p>
<p>fmin() &#x2F; fminf() &#x2F; fminl() —-求最小值</p>
<p>hypot() &#x2F; hypotf() &#x2F; hypotl() —-求直角三角形斜边的长度</p>
<p> fmod() &#x2F; fmodf() &#x2F; fmodl() —-求两数整除后的余数</p>
<p>modf() &#x2F; modff() &#x2F; modfl() —-浮点数分解为整数和小数</p>
<p>frexp() &#x2F; frexpf() &#x2F; frexpl() —-浮点数分解尾数和二为底的指数</p>
<p>sin() &#x2F; sinf() &#x2F; sinl() —-求正弦值</p>
<p>sinh() &#x2F; sinhf() &#x2F; sinhl() —-求双曲正弦值</p>
<p>cos() &#x2F; cosf() &#x2F; cosl() —-求余弦值</p>
<p>cosh() &#x2F; coshf() &#x2F; coshl() —-求双曲余弦值</p>
<p>tan() &#x2F; tanf() &#x2F; tanl() —-求正切值</p>
<p>tanh() &#x2F; tanhf() &#x2F; tanhl() —-求双曲正切值</p>
<p>asin() &#x2F; asinf() &#x2F; asinl() —-求反正弦值</p>
<p>asinh() &#x2F; asinhf() &#x2F; asinhl() —-求反双曲正弦值</p>
<p>acos() &#x2F; acosf() &#x2F; acosl() —-求反余弦值</p>
<p> acosh() &#x2F; acoshf() &#x2F; acoshl() —-求反双曲余弦值</p>
<p>atan() &#x2F; atanf() &#x2F; atanl() —-求反正切值</p>
<p>atan2() &#x2F; atan2f() &#x2F; atan2l() —-求坐标值的反正切值</p>
<p>atanh() &#x2F; atanhf() &#x2F; atanhl() —-求反双曲正切值</p>
<p><a target="_blank" rel="noopener" href="http://read.pudn.com/downloads61/ebook/211392/%E9%AB%98%E7%BA%A7%E8%AF%AD%E8%A8%80c%2B%2B%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/%E9%99%84%E5%BD%95B.pdf">http://read.pudn.com/downloads61/ebook/211392/%E9%AB%98%E7%BA%A7%E8%AF%AD%E8%A8%80c%2B%2B%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/%E9%99%84%E5%BD%95B.pdf</a></p>
<p><a target="_blank" rel="noopener" href="http://blog.csdn.net/abc649395594/article/details/44730425">http://blog.csdn.net/abc649395594/article/details/44730425</a></p>
<p><a target="_blank" rel="noopener" href="https://www.gitbook.com/book/eilianlove/objectivec-guide/details">https://www.gitbook.com/book/eilianlove/objectivec-guide/details</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">kyren</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://huluo666.gitee.io/2016/04/15/iOS 中的系统常用宏定义/">http://huluo666.gitee.io/2016/04/15/iOS 中的系统常用宏定义/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://huluo666.gitee.io">Kyren's Blog</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/iOS/">iOS</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2016/04/16/iOS%E5%BC%80%E5%8F%91%E4%B9%8BC%E8%AF%AD%E8%A8%80%E5%87%BD%E6%95%B0%E5%BA%93/"><i class="fa fa-chevron-left">  </i><span>iOS开发之C语言函数库</span></a></div><div class="next-post pull-right"><a href="/2016/04/14/m3u8%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD/"><span>Mac 网页视频下载</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2022 By kyren</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="footer_custom_text">hitokoto</div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/lib/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>